﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace LabMS.Web.User
{
    public partial class PasswordModify : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UserTypeTBX.Text = UserType;
            UserCodeTBX.Text = UserCode;
            UserNameTBX.Text = UserName;
            UserNameTBX.Attributes.Add("readOnly", "-1");
            
        }

        protected void Save_OnClick(object sender, EventArgs e)
        {
            #region
            if (String.IsNullOrEmpty(UserPasswordTBX.Text.Trim()))
            {
                Common.JShelper.JSAlert(Page, "script", "请输入旧密码!");
                return;
            }
            if (UserPasswordTBX.Text.Length > 50)
            {
                Common.JShelper.JSAlert(Page, "script", "旧密码长度输入过长!");
                return;
            }
            if (String.IsNullOrEmpty(NewPasswordTBX.Text.Trim()))
            {
                Common.JShelper.JSAlert(Page, "script", "请输入新密码密码!");
                return;
            }
            if (NewPasswordTBX.Text.Length > 50)
            {
                Common.JShelper.JSAlert(Page, "script", "新密码长度输入过长!");
                return;
            }
            if (!Regex.IsMatch(NewPasswordTBX.Text.Trim(), @"^\w+$"))
            {
                Common.JShelper.JSAlert(Page, "script", "新密码输入不合法(只能为数字、英文或下划线的组合)!");
                return;
            }
            //if (String.IsNullOrEmpty(RePasswordTBX.Text.Trim()))
            //{
            //    Common.JShelper.JSAlert(Page, "script", "请重复输入新密码!");
            //}
            //if (RePasswordTBX.Text.Length > 50)
            //{
            //    Common.JShelper.JSAlert(Page, "script", "重复新密码长度输入过长!");
            //}
            //if (!Regex.IsMatch(RePasswordTBX.Text.Trim(), @"^\w+$"))
            //{
            //    Common.JShelper.JSAlert(Page, "script", "重复新密码输入不合法(只能为数字、英文或下划线的组合)!");
            //}
            if (String.Compare(RePasswordTBX.Text, NewPasswordTBX.Text) != 0)
            {
                Common.JShelper.JSAlert(Page, "script", "两次新密码输入不一致!");
                return;
            }

            //验证用户名和密码输入是否正确
            if (!IsUserNameAndPassword())
            {
                Common.JShelper.JSAlert(Page, "script", "用户名和密码输入不匹配!");
                return;
            }
            #endregion

                if (!String.IsNullOrEmpty(UserID))
                {
                    int ID;
                    if (int.TryParse(UserID, out ID))
                    {
                        if (UserType == "mana")
                        {
                            LabMS.BLL.UserTable userTable = new LabMS.BLL.UserTable();
                            LabMS.Model.UserTable userTableModel = new LabMS.Model.UserTable();

                            userTableModel = userTable.GetModel(ID);
                            userTableModel.Password = NewPasswordTBX.Text.Trim();

                            try
                            {
                                userTable.Update(userTableModel);
                                Common.JShelper.JsAlertAndClose(Page, "script", "密码修改成功！");
                            }
                            catch
                            {
                                Common.JShelper.JsAlertAndClose(Page, "script", "密码修改失败!");
                            }
                        }
                        else if (UserType == "teacher")
                        {
                            LabMS.BLL.Teacher userTable = new LabMS.BLL.Teacher();
                            LabMS.Model.Teacher userTableModel = new LabMS.Model.Teacher();

                            userTableModel = userTable.GetModel(ID);
                            userTableModel.Password = NewPasswordTBX.Text.Trim();

                            try
                            {
                                userTable.Update(userTableModel);
                                Common.JShelper.JsAlertAndClose(Page, "script", "密码修改成功！");
                            }
                            catch
                            {
                                Common.JShelper.JsAlertAndClose(Page, "script", "密码修改失败!");
                            }
                        }
                        else
                        {
                            LabMS.BLL.Student userTable = new LabMS.BLL.Student();
                            LabMS.Model.Student userTableModel = new LabMS.Model.Student();

                            userTableModel = userTable.GetModel(ID);

                            userTableModel.Student_Pass = NewPasswordTBX.Text.Trim();

                            try
                            {
                                userTable.Update(userTableModel);
                                Common.JShelper.JsAlertAndClose(Page, "script", "密码修改成功！");
                            }
                            catch
                            {
                                Common.JShelper.JsAlertAndClose(Page, "script", "密码修改失败!");
                            }
                        }
                        
                    }
                    else
                    {
                        Common.JShelper.JsAlertAndClose(Page, "script", "用户ID存储不合法，请退出系统，重新登录后进行修改密码！");
                    }
                }
                else
                {
                    Common.JShelper.JsAlertAndClose(Page, "script", "用户Session丢失，请退出系统，重新登录后进行修改密码！");
                }


        }

        private bool IsUserNameAndPassword()
        {
            if (UserType == "mana")
            {
                LabMS.BLL.UserTable userTable = new LabMS.BLL.UserTable();
                List<LabMS.Model.UserTable> userTableModelList = new List<LabMS.Model.UserTable>();

                userTableModelList = userTable.GetModelList("Password='" + UserPasswordTBX.Text.Trim() + "' and PCode='" + UserCodeTBX.Text + "'");
                if (userTableModelList.Count == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (UserType == "teacher")
            {
                LabMS.BLL.Teacher userTable = new LabMS.BLL.Teacher();
                List<LabMS.Model.Teacher> userTableModelList = new List<LabMS.Model.Teacher>();

                userTableModelList = userTable.GetModelList("Password='" + UserPasswordTBX.Text.Trim() + "' and Teacher_Code='" + UserCodeTBX.Text + "'");
                if (userTableModelList.Count == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                LabMS.BLL.Student userTable = new LabMS.BLL.Student();
                List<LabMS.Model.Student> userTableModelList = new List<LabMS.Model.Student>();

                userTableModelList = userTable.GetModelList("Student_Pass='" + UserPasswordTBX.Text.Trim() + "' and Student_Code='" + UserCodeTBX.Text + "'");
                if (userTableModelList.Count == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }
    }
}
